package com.zz.study.sharding.util;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * created by xjx on  2024-01-31 14:59
 */
@Data
public class DateSimple {

    private Integer year;

    private Integer month;


    public DateSimple(int year, int month) {
        this.year = year;
        this.month = month;
    }

    public DateSimple(String year, String month) {
        this.year = Integer.valueOf(year);
        this.month =  Integer.valueOf(month);
    }

    @Override
    public String toString() {
        return   year.toString() + month.toString() ;
    }

    public static boolean equals(DateSimple date1, DateSimple date2){
        if(date1.year.equals(date2.year) &&(date1.month.equals(date2.month))){
            return true;
        }
        return false;
    }

    /**
     * 下一个月
     * @return
     */
    public synchronized DateSimple inc(){
        if(month==12){
            month=1;
            this.year = this.year+1;
            return this;
        }else{
            this.month = this.month+1;
            return this;
        }
    }

    /**
     *  时间范围内，包含了哪几个月
     * @return
     */
    public static List<String> dateRangeMonth(DateSimple startDate, DateSimple endDate){
        List<String> returnList = new ArrayList<>();
        if(equals(startDate,endDate)){
            returnList.add(startDate.year+""+startDate.month);
            return returnList;
        }else {
            returnList.add(startDate.year+""+startDate.month);
            while(!equals(startDate,endDate)){
                startDate = startDate.inc();
                returnList.add(startDate.year+""+startDate.month);
            }
            return returnList;
        }
    }

    public static void main(String[] args) {
        DateSimple date1 = new DateSimple(2023,12);
        DateSimple date2 = new DateSimple(2024,5);
       List<String> list =  dateRangeMonth(date1,date2);
        for (String s : list) {
            System.out.println(s);
        }
    }
}
